﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using SMALLERP.ComClass;
using System.IO;

namespace XJLSC.PM
{
    public partial class ZJHGL : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        private SqlDataReader sdr;

        public ZJHGL()
        {
            InitializeComponent();
        }

        public delegate void CheckBoxClickedHandler(bool state);
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            public bool Checked
            {
                get { return _bChecked; }
            }
        }
        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            System.Drawing.Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            System.Drawing.Point _cellLocation = new System.Drawing.Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            protected override void Paint(System.Drawing.Graphics graphics,
                System.Drawing.Rectangle clipBounds,
                System.Drawing.Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle,
                    advancedBorderStyle, paintParts);
                System.Drawing.Point p = new System.Drawing.Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                    (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y +
                    (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }

            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                System.Drawing.Point p = new System.Drawing.Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= checkBoxLocation.X && p.X <=
                    checkBoxLocation.X + checkBoxSize.Width
                && p.Y >= checkBoxLocation.Y && p.Y <=
                    checkBoxLocation.Y + checkBoxSize.Height)
                {
                    _checked = !_checked;
                    if (OnCheckBoxClicked != null)
                    {
                        OnCheckBoxClicked(_checked);
                        this.DataGridView.InvalidateCell(this);
                    }

                }
                base.OnMouseClick(e);
            }
        }


        private void abHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态

            //dataGridView1.EndEdit();
            //dataGridView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";
            DataTable dt;
            strSql = "SELECT MO_ID,MO.DOC_NO 工单单号,ITEM_CODE 主件品号 FROM MO LEFT JOIN ITEM ON MO.ITEM_ID=ITEM_BUSINESS_ID WHERE MO.[STATUS]<>'Y' AND MO.[STATUS]<>'y' AND EXISTS(SELECT * FROM DOC WHERE DOC.DOC_ID=MO.DOC_ID AND DOC.DOC_NAME NOT LIKE '%返工%')";      
            try
            {
                this.gridControl4.DataSource = "";
                this.gridControl4.Refresh();
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                this.gridControl4.DataSource = dt;
            }
            catch
            {

            }

            progressBar1.Visible = true;
            progressBar1.Maximum = gridView4.RowCount;//设置最大长度值
            progressBar1.Value = 0;//设置当前值
            progressBar1.Step = 1;//设置没次增长多少
            DataTable dta = new DataTable();
            dta.Columns.Add("订单号");
            dta.Columns.Add("工单号");
            dta.Columns.Add("主件品号");
            dta.Columns.Add("主件品名");
            dta.Columns.Add("主件规格");
            dta.Columns.Add("材料品号");
            dta.Columns.Add("材料品名");
            dta.Columns.Add("材料规格");
            dta.Columns.Add("工艺");
            dta.Columns.Add("仓库");
            dta.Columns.Add("标准数量");
            dta.Columns.Add("实际数量");
            dta.Columns.Add("单身备注");
            for (int j = 0; j <= gridView4.RowCount - 1; j++)
            {
                progressBar1.Value += progressBar1.Step;
                if (checkBox1.CheckState == CheckState.Unchecked)
                {
                    strSql = "SELECT RTRIM(MO.UDF022) 订单号,MO.DOC_NO 工单号,B.ITEM_CODE 主件品号,B.ITEM_NAME 主件品名,B.ITEM_SPECIFICATION 主件规格,C.ITEM_CODE 材料品号,C.ITEM_NAME 材料品名,C.ITEM_SPECIFICATION 材料规格,D.OPERATION_NAME 工艺,F.WAREHOUSE_NAME 仓库,CAST(QTY_PER AS REAL) 标准数量,CAST(LEAST_REQUIRED_QTY AS REAL) 实际数量,R_LX 单身备注 FROM check_Bom('" + gridView4.GetRowCellValue(j, "MO_ID") + "') A LEFT JOIN MO ON A.MO_ID=MO.MO_ID LEFT JOIN ITEM B ON MO.ITEM_ID=B.ITEM_BUSINESS_ID LEFT JOIN ITEM C ON R_YJ_ID=C.ITEM_BUSINESS_ID LEFT JOIN OPERATION D ON A.R_OPERATION_ID=D.OPERATION_ID LEFT JOIN ITEM_PLANT E ON E.Owner_Org_ROid=MO.Owner_Org_ROid AND E.ITEM_ID=C.ITEM_BUSINESS_ID LEFT JOIN WAREHOUSE F ON F.WAREHOUSE_ID=E.OUTBOUND_WAREHOUSE_ID";
                }
                else
                {
                    strSql = "SELECT RTRIM(MO.UDF022) 订单号,MO.DOC_NO 工单号,B.ITEM_CODE 主件品号,B.ITEM_NAME 主件品名,B.ITEM_SPECIFICATION 主件规格,C.ITEM_CODE 材料品号,C.ITEM_NAME 材料品名,C.ITEM_SPECIFICATION 材料规格,'****' 工艺,F.WAREHOUSE_NAME 仓库,CAST(QTY_PER AS REAL) 标准数量,CAST(LEAST_REQUIRED_QTY AS REAL) 实际数量,'' 单身备注 FROM (SELECT MO_ID,R_YJPH,SUM(QTY_PER) QTY_PER,SUM(LEAST_REQUIRED_QTY) LEAST_REQUIRED_QTY FROM check_Bom('" + gridView4.GetRowCellValue(j, "MO_ID") + "') A GROUP BY MO_ID,R_YJPH) A LEFT JOIN MO ON A.MO_ID=MO.MO_ID LEFT JOIN ITEM B ON MO.ITEM_ID=B.ITEM_BUSINESS_ID LEFT JOIN ITEM C ON R_YJPH=C.ITEM_CODE LEFT JOIN ITEM_PLANT E ON E.Owner_Org_ROid=MO.Owner_Org_ROid AND E.ITEM_ID=C.ITEM_BUSINESS_ID LEFT JOIN WAREHOUSE F ON F.WAREHOUSE_ID=E.OUTBOUND_WAREHOUSE_ID WHERE A.QTY_PER<>A.LEAST_REQUIRED_QTY";
                }
                    //textBox1.Text =strSql;
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    DataRow dr = dta.NewRow();
                    dr["订单号"] = dt.Rows[i][0].ToString();
                    dr["工单号"] = dt.Rows[i][1].ToString();
                    dr["主件品号"] = dt.Rows[i][2].ToString();
                    dr["主件品名"] = dt.Rows[i][3].ToString();
                    dr["主件规格"] = dt.Rows[i][4].ToString();
                    dr["材料品号"] = dt.Rows[i][5].ToString();
                    dr["材料品名"] = dt.Rows[i][6].ToString();
                    dr["材料规格"] = dt.Rows[i][7].ToString();
                    dr["工艺"] = dt.Rows[i][8].ToString();
                    dr["仓库"] = dt.Rows[i][9].ToString();
                    dr["标准数量"] = dt.Rows[i][10].ToString();
                    dr["实际数量"] = dt.Rows[i][11].ToString();
                    dr["单身备注"] = dt.Rows[i][12].ToString();
                    dta.Rows.Add(dr);
                }
            }
            progressBar1.Visible = false;
            gridControl3.DataSource = dta;
            gridControl3.MainView.PopulateColumns();

            //if (radioButton1.Checked)
            //{
            //    strSql = "SELECT '再计划新增' 类型,MB025 AS 品号属性,A.BZ 项目号,A.TA006 主件品号,C.MB002 主件品名,C.MB003 主件规格,A.ID 元件品号,A.MB002 元件品名,A.MB003 元件规格,CAST(A.WJSL AS NUMERIC(16,2)) 订单数量,CAST(A.XQSL AS NUMERIC(16,6)) 需求数量,0 AS 已下单数量,CAST(A.XQSL-0 AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,'' 原工单号 FROM GDMXQ A LEFT JOIN INVMB C ON A.ID=C.MB001 WHERE (MB025='M' OR MB025='S' OR MB025='P') AND NOT EXISTS(SELECT * FROM MOCTA B LEFT JOIN MOCTB ON TA001=TB001 AND TA002=TB002 WHERE A.TA006=B.TA006 AND TB003=ID)";

            //}
            //else
            //    if (radioButton5.Checked)
            //    {
            //        strSql = "SELECT '再计划增加' 类型,D.MB025 AS 品号属性,A.BZ 项目号,A.TA006 主件品号,C.MB002 主件品名,C.MB003 主件规格,A.ID 元件品号,D.MB002 元件品名,D.MB003 元件规格, CAST(A.WJSL AS NUMERIC(16,2)) 订单数量,CAST(A.XQSL AS NUMERIC(16,2)) 需求数量,CAST(ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 已下单数量,CAST(A.XQSL-ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,B.GDDH 原工单号 FROM V_GDMXQ A LEFT JOIN V_MOCTB B ON RTRIM(B.TA083)=RTRIM(A.BZ) AND A.TA006=B.TA006 AND A.ID=B.TB003 LEFT JOIN INVMB C ON C.MB001=A.TA006 LEFT JOIN INVMB D ON D.MB001=A.ID WHERE (D.MB025='M' OR D.MB025='S' OR D.MB025='P') AND ISNULL(TB004,0)>0 AND A.XQSL-ISNULL(TB004,0)>0"; 

            //    }
            //    else
            //        if (radioButton2.Checked)
            //        {
            //            strSql = "SELECT '再计划减少' 类型,D.MB025 AS 品号属性,A.BZ 项目号,A.TA006 主件品号,C.MB002 主件品名,C.MB003 主件规格,A.ID 元件品号,D.MB002 元件品名,D.MB003 元件规格, CAST(A.WJSL AS NUMERIC(16,2)) 订单数量,CAST(A.XQSL AS NUMERIC(16,2)) 需求数量,CAST(ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 已下单数量,CAST(A.XQSL-ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,B.GDDH 原工单号 FROM V_GDMXQ A LEFT JOIN V_MOCTB B ON RTRIM(B.TA083)=RTRIM(A.BZ) AND A.TA006=B.TA006 AND A.ID=B.TB003 LEFT JOIN INVMB C ON C.MB001=A.TA006 LEFT JOIN INVMB D ON D.MB001=A.ID WHERE (D.MB025='M' OR D.MB025='S' OR D.MB025='P') AND ISNULL(TB004,0)>0 AND A.XQSL-ISNULL(TB004,0)<0";
            //        }
            //        else
            //            if (radioButton3.Checked)
            //        {
            //            strSql = "SELECT '再计划取消' 类型,MB025 AS 品号属性,A.TA083 项目号,A.TA006 主件品号,MB002 主件品名,MB003 主件规格,B.TB003 元件品号,B.TB012 元件品名,B.TB013 元件规格, CAST(A.TA015 AS NUMERIC(16,2)) 订单数量,CAST(B.TB004 AS NUMERIC(16,2)) 需求数量,CAST(ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 已下单数量,CAST(0-ISNULL(TB004,0) AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,B.TB001+'-'+B.TB002 原工单号 FROM MOCTA A LEFT JOIN MOCTB B ON A.TA001=B.TB001 AND A.TA002=B.TB002 LEFT JOIN INVMB ON TB003=MB001 WHERE  TA011<>'Y' AND TA011<>'y' AND (MB025='M' OR MB025='S' OR MB025='P') AND B.TB004>0 AND NOT EXISTS(SELECT * FROM GDMXQ C WHERE C.TA001=A.TA001 AND C.TA002=A.TA002 AND B.TB003=C.ID)";
            //        }
            //            else
            //                if (radioButton4.Checked)
            //                {
            //                    strSql = "SELECT '再计划新增' 类型,MB025 AS 品号属性,A.BZ 项目号,A.TA006 主件品号,C.MB002 主件品名,C.MB003 主件规格,A.ID 元件品号,A.MB002 元件品名,A.MB003 元件规格,CAST(A.WJSL AS NUMERIC(16,2)) 订单数量,CAST(A.XQSL AS NUMERIC(16,6)) 需求数量,0 AS 已下单数量,CAST(A.XQSL-0 AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,'' 原工单号 FROM GDMXQ A LEFT JOIN INVMB C ON A.ID=C.MB001 WHERE (MB025='M' OR MB025='S' OR MB025='P') AND NOT EXISTS(SELECT * FROM MOCTA B LEFT JOIN MOCTB ON TA001=TB001 AND TA002=TB002 WHERE A.TA006=B.TA006 AND TB003=ID) UNION ALL SELECT '再计划增加' 类型,D.MB025 AS 品号属性,A.BZ 项目号,A.TA006 主件品号,C.MB002 主件品名,C.MB003 主件规格,A.ID 元件品号,D.MB002 元件品名,D.MB003 元件规格, CAST(A.WJSL AS NUMERIC(16,2)) 订单数量,CAST(A.XQSL AS NUMERIC(16,2)) 需求数量,CAST(ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 已下单数量,CAST(A.XQSL-ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,B.GDDH 原工单号 FROM V_GDMXQ A LEFT JOIN V_MOCTB B ON RTRIM(B.TA083)=RTRIM(A.BZ) AND A.TA006=B.TA006 AND A.ID=B.TB003 LEFT JOIN INVMB C ON C.MB001=A.TA006 LEFT JOIN INVMB D ON D.MB001=A.ID WHERE (D.MB025='M' OR D.MB025='S' OR D.MB025='P') AND ISNULL(TB004,0)>0 AND A.XQSL-ISNULL(TB004,0)>0 UNION ALL SELECT '再计划减少' 类型,D.MB025 AS 品号属性,A.BZ 项目号,A.TA006 主件品号,C.MB002 主件品名,C.MB003 主件规格,A.ID 元件品号,D.MB002 元件品名,D.MB003 元件规格, CAST(A.WJSL AS NUMERIC(16,2)) 订单数量,CAST(A.XQSL AS NUMERIC(16,2)) 需求数量,CAST(ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 已下单数量,CAST(A.XQSL-ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,B.GDDH 原工单号 FROM V_GDMXQ A LEFT JOIN V_MOCTB B ON RTRIM(B.TA083)=RTRIM(A.BZ) AND A.TA006=B.TA006 AND A.ID=B.TB003 LEFT JOIN INVMB C ON C.MB001=A.TA006 LEFT JOIN INVMB D ON D.MB001=A.ID WHERE (D.MB025='M' OR D.MB025='S' OR D.MB025='P') AND ISNULL(TB004,0)>0 AND A.XQSL-ISNULL(TB004,0)<0  UNION ALL SELECT '再计划取消' 类型,MB025 AS 品号属性,A.TA083 项目号,A.TA006 主件品号,MB002 主件品名,MB003 主件规格,B.TB003 元件品号,B.TB012 元件品名,B.TB013 元件规格, CAST(A.TA015 AS NUMERIC(16,2)) 订单数量,CAST(B.TB004 AS NUMERIC(16,2)) 需求数量,CAST(ISNULL(B.TB004,0) AS NUMERIC(16,2)) AS 已下单数量,CAST(0-ISNULL(TB004,0) AS NUMERIC(16,2)) AS 差异数量,A.TA009 预计开工日,A.TA010 预计完工日,B.TB001+'-'+B.TB002 原工单号 FROM MOCTA A LEFT JOIN MOCTB B ON A.TA001=B.TB001 AND A.TA002=B.TB002 LEFT JOIN INVMB ON TB003=MB001 WHERE TA011<>'Y' AND TA011<>'y' AND (MB025='M' OR MB025='S' OR MB025='P') AND B.TB004>0 AND NOT EXISTS(SELECT * FROM GDMXQ C WHERE C.TA001=A.TA001 AND C.TA002=A.TA002 AND B.TB003=C.ID)";
            //                }

            //try
            //{
            //    dataGridView1.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
            //    //this.dataGridView4.Columns["图片"].Visible = false;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}

        }

        private void ZJHGL_Load(object sender, EventArgs e)
        {
            //DataGridViewCheckBoxColumn colCA1 = new DataGridViewCheckBoxColumn();
            //DatagridViewCheckBoxHeaderCell a1bHeader = new DatagridViewCheckBoxHeaderCell();
            //colCA1.HeaderCell = a1bHeader;
            //colCA1.Name = "colCA1";
            //colCA1.HeaderText = "";
            //dataGridView1.Columns.Insert(0, colCA1);
            //a1bHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //if ((int)MessageBox.Show("是否确定进行再计划处理？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == 6)
            //{
            //    string qgdh = "";
            //    for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //    {
            //        if (Convert.ToBoolean(this.dataGridView1.Rows[i].Cells["colCA1"].Value))
            //        {
            //            string ph = (dataGridView1.Rows[i].Cells["主件品号"].Value == null) ? "" : dataGridView1.Rows[i].Cells["主件品号"].Value.ToString().Trim();
            //            string yjcl = (dataGridView1.Rows[i].Cells["订单数量"].Value == null) ? "0" : dataGridView1.Rows[i].Cells["订单数量"].Value.ToString().Trim();
            //            string dddb = "";
            //            string dddh = "";
            //            string ddxh = "";
            //            string ddzxh = "";
            //            string cppm = (dataGridView1.Rows[i].Cells["主件品名"].Value == null) ? "" : dataGridView1.Rows[i].Cells["主件品名"].Value.ToString().Trim();
            //            string cpgg = (dataGridView1.Rows[i].Cells["主件规格"].Value == null) ? "" : dataGridView1.Rows[i].Cells["主件规格"].Value.ToString().Trim();
            //            string kgr = (dataGridView1.Rows[i].Cells["预计开工日"].Value == null) ? "" : dataGridView1.Rows[i].Cells["预计开工日"].Value.ToString().Trim();
            //            string wgr = (dataGridView1.Rows[i].Cells["预计完工日"].Value == null) ? "" : dataGridView1.Rows[i].Cells["预计完工日"].Value.ToString().Trim();
            //            string xmh = (dataGridView1.Rows[i].Cells["项目号"].Value == null) ? "" : dataGridView1.Rows[i].Cells["项目号"].Value.ToString().Trim();
                       
            //            string yjph = (dataGridView1.Rows[i].Cells["元件品号"].Value == null) ? "" : dataGridView1.Rows[i].Cells["元件品号"].Value.ToString().Trim();
            //             string yjpm = (dataGridView1.Rows[i].Cells["元件品名"].Value == null) ? "" : dataGridView1.Rows[i].Cells["元件品名"].Value.ToString().Trim();
            //            string yjgg = (dataGridView1.Rows[i].Cells["元件规格"].Value == null) ? "" : dataGridView1.Rows[i].Cells["元件规格"].Value.ToString().Trim();
            //            string xqsl = (dataGridView1.Rows[i].Cells["差异数量"].Value == null) ? "0" : dataGridView1.Rows[i].Cells["差异数量"].Value.ToString().Trim();
            //             string phsx = (dataGridView1.Rows[i].Cells["品号属性"].Value == null) ? "" : dataGridView1.Rows[i].Cells["品号属性"].Value.ToString().Trim();
            //             string gddh = (dataGridView1.Rows[i].Cells["原工单号"].Value == null) ? "" : dataGridView1.Rows[i].Cells["原工单号"].Value.ToString().Trim();
            //            string leixing = (dataGridView1.Rows[i].Cells["类型"].Value == null) ? "" : dataGridView1.Rows[i].Cells["类型"].Value.ToString().Trim();

            //            if (leixing == "再计划新增")
            //            {
            //                string strSql = "SELECT CONVERT(CHAR(8),GETDATE(),112)+  ISNULL(right('000'+rtrim(cast(cast(MAX(SUBSTRING(TA002,9,3)) as numeric(16))+1 as char(3))),3),'001') AS TA002  from MOCTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'  AND TA001='5101'";
            //                string dh = "";
            //                try
            //                {
            //                    sdr = db.GetDataReader1(strSql);
            //                    sdr.Read();

            //                    if (sdr.HasRows)
            //                    {
            //                        dh = sdr["TA002"].ToString();

            //                    }
            //                }
            //                catch
            //                {

            //                }
            //                finally
            //                {
            //                    sdr.Close();
            //                }

            //                strSql = "INSERT INTO MOCTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA024,TA025,TA026,TA027,TA028,TA030,TA033,TA034,TA035,TA057,TA083,UDF01) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'5101','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),CONVERT(CHAR(8),GETDATE(),112),'0000','" + ph + "',MB004,'',REPLACE('" + kgr + "','-',''),REPLACE('" + wgr + "','-',''),'1','','N',''," + xqsl + ",0,0,0,'01',MB017,MB068,'5101','" + dh + "','" + dddb + "','" + dddh + "','" + ddxh + "','1','','" + yjpm + "','" + yjgg + "','********************','" + xmh + "','" + ddzxh + "' FROM INVMB WHERE MB001='" + ph + "' AND NOT EXISTS(SELECT * FROM MOCTA WHERE TA083='" + xmh + "' AND TA006='" + ph + "')";
                           
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }

            //                strSql = "INSERT INTO MOCTB(COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB005,TB006,TB007,TB008,TB009,TB010,TB011,TB012,TB013,TB014,TB015,TB016,TB017,TB018,TB026,TB027,TB028) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,TA001,TA002,'" + yjph + "'," + xqsl + ",0,'****',MB149,'',MB017,'','1','" + yjpm + "','" + yjgg + "','" + ph + "','" + kgr + "','','','N',0," + xqsl + ",'' FROM MOCTA,INVMB WHERE TA083='" + xmh + "' AND TA006='" + ph + "' AND MB001='" + yjph + "'";
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }

            //                if(phsx=="M")
            //                {
            //                  strSql = "SELECT CONVERT(CHAR(8),GETDATE(),112)+  ISNULL(right('000'+rtrim(cast(cast(MAX(SUBSTRING(TA002,9,3)) as numeric(16))+1 as char(3))),3),'001') AS TA002  from MOCTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'  AND TA001='5101'";
            //                    try
            //                    {
            //                        sdr = db.GetDataReader1(strSql);
            //                        sdr.Read();

            //                        if (sdr.HasRows)
            //                        {
            //                            dh = sdr["TA002"].ToString();

            //                        }
            //                    }
            //                    catch
            //                    {

            //                    }
            //                    finally
            //                    {
            //                        sdr.Close();
            //                    }

            //                    strSql = "INSERT INTO MOCTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA024,TA025,TA026,TA027,TA028,TA030,TA033,TA034,TA035,TA057,TA083,UDF01) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'5101','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),CONVERT(CHAR(8),GETDATE(),112),'0000','" + yjph + "',MB004,'',REPLACE('" + kgr + "','-',''),REPLACE('" + wgr + "','-',''),'1','','N',''," + xqsl + ",0,0,0,'01',MB017,MB068,'5101','" + dh + "','" + dddb + "','" + dddh + "','" + ddxh + "','1','','" + yjpm + "','" + yjgg + "','********************','" + xmh + "','" + ddzxh + "' FROM INVMB WHERE MB001='" + yjph + "' AND NOT EXISTS(SELECT * FROM MOCTA WHERE TA083='" + xmh + "' AND TA006='" + yjph + "')";

            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }

                               
            //                }

            //                if (phsx == "S")
            //                {
            //                    strSql = "SELECT CONVERT(CHAR(8),GETDATE(),112)+  ISNULL(right('000'+rtrim(cast(cast(MAX(SUBSTRING(TA002,9,3)) as numeric(16))+1 as char(3))),3),'001') AS TA002  from MOCTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'  AND TA001='5121'";
            //                    try
            //                    {
            //                        sdr = db.GetDataReader1(strSql);
            //                        sdr.Read();

            //                        if (sdr.HasRows)
            //                        {
            //                            dh = sdr["TA002"].ToString();

            //                        }
            //                    }
            //                    catch
            //                    {

            //                    }
            //                    finally
            //                    {
            //                        sdr.Close();
            //                    }

            //                    strSql = "INSERT INTO MOCTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA024,TA025,TA026,TA027,TA028,TA030,TA033,TA034,TA035,TA057,TA083,UDF01) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'5121','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),CONVERT(CHAR(8),GETDATE(),112),'0000','" + yjph + "',MB004,'',REPLACE('" + kgr + "','-',''),REPLACE('" + wgr + "','-',''),'1','','N',''," + xqsl + ",0,0,0,'01',MB017,'','5101','" + dh + "','" + dddb + "','" + dddh + "','" + ddxh + "','1','','" + yjpm + "','" + yjgg + "','********************','" + xmh + "','" + ddzxh + "' FROM INVMB WHERE MB001='" + yjph + "' AND NOT EXISTS(SELECT * FROM MOCTA WHERE TA083='" + xmh + "' AND TA006='" + yjph + "')";

            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }

                               
            //                }

            //                if (phsx == "P")
            //                {
            //                    if (qgdh == "")
            //                    {
            //                        strSql = "SELECT ISNULL(CAST(CAST(MAX(TA002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),GETDATE(),112)+'001') AS 单号 FROM PURTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%' AND TA001 ='3101'";
            //                        try
            //                        {
            //                            sdr1 = db.GetDataReader1(strSql);

            //                            while (sdr1.Read())
            //                            {
            //                                qgdh = sdr1["单号"].ToString();

            //                            }

            //                            if (sdr1.HasRows == false)
            //                            {

            //                            }

            //                        }

            //                        catch (Exception ex)
            //                        {
            //                            MessageBox.Show(ex.Message, "软件提示");
            //                            throw ex;
            //                        }
            //                        finally
            //                        {
            //                            sdr1.Close();
            //                        }

            //                        strSql = "INSERT INTO PURTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA022,TA023,TA024,TA025,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60,UDF61,UDF62) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','3101','" + qgdh + "',CONVERT(CHAR(8),GETDATE(),112),'76000','','','Y',0,'3','01',0,'',CONVERT(CHAR(8),GETDATE(),112),'" + PropertyClass.OperatorCode + "',0,'N',0,'','','',0,0,0,0,'" + xmh + "','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 WHERE NOT EXISTS(SELECT * FROM PURTA WHERE TA001='3101' AND TA002='" + qgdh + "')";


            //                        try
            //                        {
            //                            if (db.ExecDataBySql1(strSql) > 0)
            //                            {
            //                            }
            //                            else
            //                            {
            //                                MessageBox.Show("生成请购单单头失败，请检查", "软件提示");
            //                                return;
            //                            }
            //                        }
            //                        catch
            //                        {
            //                            //MessageBox.Show(ex.Message, "软件提示");
            //                            //throw ex;
            //                        }

            //                    }

            //                            strSql = "INSERT INTO PURTB (COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB005,TB006,TB007,TB008,TB009,TB010,TB011,TB012,TB013,TB014,TB015,TB016,TB017,TB018,TB019,TB020,TB021,TB022,TB023,TB024,TB025,TB026,TB027,TB028,TB029,TB030,TB031,TB032,TB033,TB034,TB035,TB036,TB037,TB038,TB039,TB040,TB041,TB042,TB043,TB044,TB045,TB046,TB047,TB048,TB049,TB050,TB051,TB052,TB053,TB054,TB055,TB056,TBC01,TB057,TB058,TB059,TB060,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60,UDF61,UDF62) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','3101','" + qgdh + "',(SELECT RIGHT(RTRIM('000'+ISNULL(CAST(CAST(MAX(TB003) AS NUMERIC(4))+1 AS CHAR(4)),'0001')),4) FROM PURTB WHERE TB001='3101' AND TB002='" + qgdh + "'),";
            //                            strSql = strSql + "'" + yjph + "','" + yjpm + "','" + yjgg + "',MB004,MB017," + xqsl + ",MB032,'" + kgr + "','','',0,MB004,'RMB',0,0,'" + kgr + "','N','N','','','','Y','1','','','','','','N','',0,0,'','','','N',0,0,'',MB149," + xqsl + ",0,0,0," + xqsl + ",'',CONVERT(CHAR(8),GETDATE(),112),'',0,0,0,'',0,'2','','','','" + xmh + "','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 FROM INVMB WHERE MB001='" + yjph + "'";
            //                            //textBox2.Text = strSql;
            //                            //    return;
            //                            try
            //                            {
            //                                if (db.ExecDataBySql1(strSql) > 0)
            //                                {
            //                                }
            //                                else
            //                                {
            //                                    //MessageBox.Show("生成领料单单身失败，请检查", "软件提示");
            //                                    //return;
            //                                }
            //                            }
            //                            catch (Exception ex)
            //                            {
            //                                MessageBox.Show(ex.Message, "软件提示");
            //                                throw ex;
            //                            }
                                    

            //                    strSql = "INSERT INTO PURTR (COMPANY,CREATOR,CREATE_DATE,FLAG,TR001,TR002,TR003,TR004,TR005,TR006,TR007,TR008,TR009,TR010,TR011,TR012,TR013,TR014,TR015,TR016,TR017,TR018,TR019,TR020,TR021,TR022,TR023,TR024,TR025,TR026,TR027,TR028,TR029,TR030,TR031,TR032,TR033,TR034) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1',TB001,TB002,TB003,";
            //                    strSql = strSql + "'0001',MB032,TB009,1,MB004,TB016,TB017,TB018,'2',TB019,MB017,'N','','N','N','',0,'',0,0,'','','Kg',TB009,'','','',0,0,0,'' FROM PURTB LEFT JOIN INVMB ON TB004=MB001 WHERE TB001='3101' AND TB002='" + qgdh + "' AND NOT EXISTS(SELECT * FROM PURTR WHERE TR001=TB001 AND TR002=TB002 AND TR003=TB003)";
            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {
            //                        }
            //                        else
            //                        {
            //                            //MessageBox.Show("生成领料单单身失败，请检查", "软件提示");
            //                            //return;
            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }

            //                }

            //            }

            //            if (leixing == "再计划增加")
            //            {
            //                string dh = "";
            //                string strSql = "UPDATE MOCTB SET TB004=TB004+" + xqsl + " FROM MOCTB WHERE TB001+'-'+TB002='" + gddh + "' AND TB003='" + yjph + "'";
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }

            //                if (phsx == "M")
            //                {
            //                    strSql = "SELECT CONVERT(CHAR(8),GETDATE(),112)+  ISNULL(right('000'+rtrim(cast(cast(MAX(SUBSTRING(TA002,9,3)) as numeric(16))+1 as char(3))),3),'001') AS TA002  from MOCTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'  AND TA001='5101'";
            //                    try
            //                    {
            //                        sdr = db.GetDataReader1(strSql);
            //                        sdr.Read();

            //                        if (sdr.HasRows)
            //                        {
            //                            dh = sdr["TA002"].ToString();

            //                        }
            //                    }
            //                    catch
            //                    {

            //                    }
            //                    finally
            //                    {
            //                        sdr.Close();
            //                    }

            //                    strSql = "INSERT INTO MOCTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA024,TA025,TA026,TA027,TA028,TA030,TA033,TA034,TA035,TA057,TA083,UDF01) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'5101','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),CONVERT(CHAR(8),GETDATE(),112),'0000','" + yjph + "',MB004,'',REPLACE('" + kgr + "','-',''),REPLACE('" + wgr + "','-',''),'1','','N',''," + xqsl + ",0,0,0,'01',MB017,MB068,'5101','" + dh + "','" + dddb + "','" + dddh + "','" + ddxh + "','1','','" + yjpm + "','" + yjgg + "','********************','" + xmh + "','" + ddzxh + "' FROM INVMB WHERE MB001='" + yjph + "'";
                                
            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }


            //                }

            //                if (phsx == "S")
            //                {
            //                    strSql = "SELECT CONVERT(CHAR(8),GETDATE(),112)+  ISNULL(right('000'+rtrim(cast(cast(MAX(SUBSTRING(TA002,9,3)) as numeric(16))+1 as char(3))),3),'001') AS TA002  from MOCTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'  AND TA001='5121'";
            //                    try
            //                    {
            //                        sdr = db.GetDataReader1(strSql);
            //                        sdr.Read();

            //                        if (sdr.HasRows)
            //                        {
            //                            dh = sdr["TA002"].ToString();

            //                        }
            //                    }
            //                    catch
            //                    {

            //                    }
            //                    finally
            //                    {
            //                        sdr.Close();
            //                    }

            //                    strSql = "INSERT INTO MOCTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA024,TA025,TA026,TA027,TA028,TA030,TA033,TA034,TA035,TA057,TA083,UDF01) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'5121','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),CONVERT(CHAR(8),GETDATE(),112),'0000','" + yjph + "',MB004,'',REPLACE('" + kgr + "','-',''),REPLACE('" + wgr + "','-',''),'','1','','N'," + xqsl + ",0,0,0,'01',MB017,'','5101','" + dh + "','" + dddb + "','" + dddh + "','" + ddxh + "','1','','" + yjpm + "','" + yjgg + "','********************','" + xmh + "','" + ddzxh + "' FROM INVMB WHERE MB001='" + yjph + "'";

            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {

            //                        }
            //                        else
            //                        {

            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }


            //                }

            //                if (phsx == "P")
            //                {
            //                    if (qgdh == "")
            //                    {
            //                        strSql = "SELECT ISNULL(CAST(CAST(MAX(TA002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),GETDATE(),112)+'001') AS 单号 FROM PURTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%' AND TA001 ='3101'";
            //                        try
            //                        {
            //                            sdr1 = db.GetDataReader1(strSql);

            //                            while (sdr1.Read())
            //                            {
            //                                qgdh = sdr1["单号"].ToString();

            //                            }

            //                            if (sdr1.HasRows == false)
            //                            {

            //                            }

            //                        }

            //                        catch (Exception ex)
            //                        {
            //                            MessageBox.Show(ex.Message, "软件提示");
            //                            throw ex;
            //                        }
            //                        finally
            //                        {
            //                            sdr1.Close();
            //                        }

            //                        strSql = "INSERT INTO PURTA(COMPANY,CREATOR,CREATE_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA022,TA023,TA024,TA025,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60,UDF61,UDF62) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','3101','" + qgdh + "',CONVERT(CHAR(8),GETDATE(),112),'76000','','','Y',0,'3','01',0,'',CONVERT(CHAR(8),GETDATE(),112),'" + PropertyClass.OperatorCode + "',0,'N',0,'','','',0,0,0,0,'" + xmh + "','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 WHERE NOT EXISTS(SELECT * FROM PURTA WHERE TA001='3101' AND TA002='" + qgdh + "')";


            //                        try
            //                        {
            //                            if (db.ExecDataBySql1(strSql) > 0)
            //                            {
            //                            }
            //                            else
            //                            {
            //                                MessageBox.Show("生成请购单单头失败，请检查", "软件提示");
            //                                return;
            //                            }
            //                        }
            //                        catch
            //                        {
            //                            //MessageBox.Show(ex.Message, "软件提示");
            //                            //throw ex;
            //                        }

            //                    }

            //                    strSql = "INSERT INTO PURTB (COMPANY,CREATOR,CREATE_DATE,FLAG,TB001,TB002,TB003,TB004,TB005,TB006,TB007,TB008,TB009,TB010,TB011,TB012,TB013,TB014,TB015,TB016,TB017,TB018,TB019,TB020,TB021,TB022,TB023,TB024,TB025,TB026,TB027,TB028,TB029,TB030,TB031,TB032,TB033,TB034,TB035,TB036,TB037,TB038,TB039,TB040,TB041,TB042,TB043,TB044,TB045,TB046,TB047,TB048,TB049,TB050,TB051,TB052,TB053,TB054,TB055,TB056,TBC01,TB057,TB058,TB059,TB060,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60,UDF61,UDF62) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1','3101','" + qgdh + "',(SELECT RIGHT(RTRIM('000'+ISNULL(CAST(CAST(MAX(TB003) AS NUMERIC(4))+1 AS CHAR(4)),'0001')),4) FROM PURTB WHERE TB001='3101' AND TB002='" + qgdh + "'),";
            //                    strSql = strSql + "'" + yjph + "','" + yjpm + "','" + yjgg + "',MB004,MB017," + xqsl + ",MB032,'" + kgr + "','','',0,MB004,'RMB',0,0,'" + kgr + "','N','N','','','','Y','1','','','','','','N','',0,0,'','','','N',0,0,'',MB149," + xqsl + ",0,0,0," + xqsl + ",'',CONVERT(CHAR(8),GETDATE(),112),'',0,0,0,'',0,'2','','','','" + xmh + "','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 FROM INVMB WHERE MB001='" + yjph + "'";
            //                    //textBox2.Text = strSql;
            //                    //    return;
            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {
            //                        }
            //                        else
            //                        {
            //                            //MessageBox.Show("生成领料单单身失败，请检查", "软件提示");
            //                            //return;
            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }


            //                    strSql = "INSERT INTO PURTR (COMPANY,CREATOR,CREATE_DATE,FLAG,TR001,TR002,TR003,TR004,TR005,TR006,TR007,TR008,TR009,TR010,TR011,TR012,TR013,TR014,TR015,TR016,TR017,TR018,TR019,TR020,TR021,TR022,TR023,TR024,TR025,TR026,TR027,TR028,TR029,TR030,TR031,TR032,TR033,TR034) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'1',TB001,TB002,TB003,";
            //                    strSql = strSql + "'0001',MB032,TB009,1,MB004,TB016,TB017,TB018,'2',TB019,MB017,'N','','N','N','',0,'',0,0,'','','Kg',TB009,'','','',0,0,0,'' FROM PURTB LEFT JOIN INVMB ON TB004=MB001 WHERE TB001='3101' AND TB002='" + qgdh + "' AND NOT EXISTS(SELECT * FROM PURTR WHERE TR001=TB001 AND TR002=TB002 AND TR003=TB003)";
            //                    try
            //                    {
            //                        if (db.ExecDataBySql1(strSql) > 0)
            //                        {
            //                        }
            //                        else
            //                        {
            //                            //MessageBox.Show("生成领料单单身失败，请检查", "软件提示");
            //                            //return;
            //                        }
            //                    }
            //                    catch (Exception ex)
            //                    {
            //                        MessageBox.Show(ex.Message, "软件提示");
            //                        throw ex;
            //                    }

            //                }
            //            }

            //            if (leixing == "再计划减少")
            //            {
            //                string strSql = "UPDATE MOCTB SET TB004=TB004+" + xqsl + " FROM MOCTB WHERE TB001+'-'+TB002='" + gddh + "' AND TB003='" + yjph + "'";
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }
            //            }

            //            if (leixing == "再计划取消")
            //            {
            //                string strSql = "UPDATE MOCTB SET TB004=0 FROM MOCTB WHERE TB001+'-'+TB002='" + gddh + "' AND TB003='" + yjph + "'";
            //                //MessageBox.Show(strSql,"123");
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }

            //                strSql = "UPDATE MOCTA SET TA011='y' WHERE TA006='" + yjph + "' AND TA083='" + xmh + "'";
            //                //MessageBox.Show(strSql,"123");
            //                try
            //                {
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {

            //                    }
            //                    else
            //                    {

            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }
            //            }

            //        }
            //    }
            //    button1.PerformClick();
            //}
           
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //SaveFileDialog dlg = new SaveFileDialog();
            //dlg.Filter = "Execl files (*.xls)|*.xls";
            //dlg.FilterIndex = 0;
            //dlg.RestoreDirectory = true;
            //dlg.CreatePrompt = true;
            //dlg.Title = "保存为Excel文件";
            //if (dlg.ShowDialog() == DialogResult.OK)
            //{
            //    Stream myStream;
            //    myStream = dlg.OpenFile();
            //    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            //    string columnTitle = null;
            //    try
            //    {
            //        //写入列标题    
            //        for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //        {
            //            if (dataGridView1.Columns[i].Visible == true)
            //            {
            //                if (i > 0)
            //                {
            //                    columnTitle += "\t";
            //                }
            //                columnTitle += dataGridView1.Columns[i].HeaderText;

            //            }

            //        }
            //        sw.WriteLine(columnTitle);
            //        //写入列内容    
            //        for (int j = 0; j < dataGridView1.Rows.Count; j++)
            //        {

            //            string columnValue = null;
            //            for (int k = 0; k < dataGridView1.Columns.Count; k++)
            //            {
            //                if (dataGridView1.Columns[k].Visible == true)
            //                {
            //                    if (k > 0)
            //                    {
            //                        columnValue += "\t";
            //                    }
            //                    if (dataGridView1.Rows[j].Cells[k].Value == null)
            //                        columnValue += "";
            //                    else
            //                        columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
            //                }
            //            }
            //            sw.WriteLine(columnValue);
            //        }
            //        sw.Close();
            //        myStream.Close();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.ToString());
            //    }
            //    finally
            //    {
            //        sw.Close();
            //        myStream.Close();
            //    }
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String strSql = "SELECT Z.PURCHASE_ORDER_SD_ID AS ID,RTRIM(C.SUPPLIER_NAME) 供应商,B.DOC_NO 采购单号,A.SequenceNumber 序号,D.ITEM_CODE 品号,D.ITEM_NAME 品名,D.ITEM_SPECIFICATION 规格,CAST(Z.BUSINESS_QTY AS REAL) 数量,CAST(Z.RECEIPTED_BUSINESS_QTY AS REAL) 已交数量,Z.PLAN_ARRIVAL_DATE 预交货日,A.REMARK 备注,A.UDF021 客户单号 ";
                   strSql += "FROM PURCHASE_ORDER_SD Z LEFT JOIN PURCHASE_ORDER_D A ON A.PURCHASE_ORDER_D_ID = Z.PURCHASE_ORDER_D_ID LEFT JOIN PURCHASE_ORDER B ON A.PURCHASE_ORDER_ID = B.PURCHASE_ORDER_ID LEFT JOIN SUPPLIER C ON C.SUPPLIER_BUSINESS_ID = B.SUPPLIER_ID ";
                   strSql += " LEFT JOIN ITEM D ON D.ITEM_BUSINESS_ID = A.ITEM_ID WHERE Z.RECEIPT_CLOSE = '0' AND A.UDF021 LIKE '%" + textBox1.Text + "%' AND '" + textBox1.Text + "'<>'' ORDER BY SUPPLIER_CODE,B.DOC_NO";
            //textBox2.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl1.DataSource = "";
                this.gridControl1.Refresh();
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                this.gridControl1.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if(textBox2.Text=="")
            {
                MessageBox.Show("请输入变更原因！", "软件提示");
                return;
            }

            int[] rows = this.gridView1.GetSelectedRows();
            foreach (int row in rows)
            {
                String cgdh = this.gridView1.GetRowCellValue(row, "ID").ToString();
                String strSql = "";
                strSql = "UPDATE PURCHASE_ORDER_SD SET RECEIPT_CLOSE = '1',REMARK=REMARK+'" + textBox2.Text + "' WHERE PURCHASE_ORDER_SD.PURCHASE_ORDER_SD_ID='" + cgdh + "' AND '" + cgdh + "'<>''";
             
                try
                {

                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            MessageBox.Show("指定结束成功，请认真核对！", "软件提示");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            String strSql = "SELECT A.PURCHASE_ORDER_D_ID AS ID,RTRIM(C.SUPPLIER_NAME) 供应商,B.DOC_NO 采购单号,A.SequenceNumber 序号,D.ITEM_CODE 品号,D.ITEM_NAME 品名,D.ITEM_SPECIFICATION 规格,CAST(Z.BUSINESS_QTY AS REAL) 数量,CAST(Z.RECEIPTED_BUSINESS_QTY AS REAL) 已交数量,CAST(E.PLAN_QTY AS REAL) 工单数量,Z.PLAN_ARRIVAL_DATE 预交货日,A.REMARK 备注,A.UDF021 客户单号 ";
            strSql += "FROM PURCHASE_ORDER_SD Z LEFT JOIN PURCHASE_ORDER_D A ON A.PURCHASE_ORDER_D_ID = Z.PURCHASE_ORDER_D_ID LEFT JOIN PURCHASE_ORDER B ON A.PURCHASE_ORDER_ID = B.PURCHASE_ORDER_ID LEFT JOIN SUPPLIER C ON C.SUPPLIER_BUSINESS_ID = B.SUPPLIER_ID ";
            strSql += " LEFT JOIN ITEM D ON D.ITEM_BUSINESS_ID = A.ITEM_ID LEFT JOIN MO E ON E.MO_ID=A.SOURCE_ID_ROid WHERE A.PURCHASE_TYPE='2' AND A.BUSINESS_QTY<>E.PLAN_QTY AND Z.RECEIPT_CLOSE = '0' AND A.UDF021 LIKE '%" + textBox1.Text + "%' ORDER BY SUPPLIER_CODE,B.DOC_NO";
            //textBox2.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl2.DataSource = "";
                this.gridControl2.Refresh();
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                this.gridControl2.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("请输入变更原因！", "软件提示");
                return;
            }

            int[] rows = this.gridView2.GetSelectedRows();
            foreach (int row in rows)
            {
                String cgdh = this.gridView2.GetRowCellValue(row, "ID").ToString();
                String strSql = "";
                strSql = "UPDATE PURCHASE_ORDER_D SET BUSINESS_QTY=MO.PLAN_QTY,INVENTORY_QTY=MO.PLAN_QTY FROM MO WHERE MO.MO_ID=PURCHASE_ORDER_D.SOURCE_ID_ROid AND PURCHASE_TYPE='2' AND PURCHASE_ORDER_D.SOURCE_ID_RTK='MO' AND PURCHASE_ORDER_D.BUSINESS_QTY<>MO.PLAN_QTY AND PURCHASE_ORDER_D.PURCHASE_ORDER_D_ID='" + cgdh + "'";
                //textBox4.Text = strSql;
                try
                {

                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }

                strSql = "UPDATE PURCHASE_ORDER_SD SET BUSINESS_QTY=PURCHASE_ORDER_D.BUSINESS_QTY,INVENTORY_QTY=PURCHASE_ORDER_D.INVENTORY_QTY,PRICE_QTY=PURCHASE_ORDER_D.PRICE_QTY FROM PURCHASE_ORDER_D ";
                strSql += "WHERE PURCHASE_ORDER_D.PURCHASE_ORDER_D_ID = PURCHASE_ORDER_SD.PURCHASE_ORDER_D_ID AND PURCHASE_ORDER_SD.BUSINESS_QTY<> PURCHASE_ORDER_D.BUSINESS_QTY AND PURCHASE_ORDER_D.PURCHASE_TYPE = '2' AND PURCHASE_ORDER_D.PURCHASE_ORDER_D_ID='" + cgdh + "'";              
                try
                {

                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            MessageBox.Show("同步成功，请认真核对！", "软件提示");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            String strSql = "SELECT B.DOC_NO 工单单号,ISNULL(PROJECT_CODE,'') 项目号,D.ITEM_CODE 材料品号,D.ITEM_NAME 材料品名,D.ITEM_SPECIFICATION 材料规格,ISNULL(C.OPERATION_NAME,'') 工艺,CAST(A.REQUIRED_QTY AS REAL) 需求数量,CAST((SELECT KCSL FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 库存数量,CAST((SELECT AQKC FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 安全库存,CAST((SELECT DDSL FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 预计销售,CAST((SELECT YJLL FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 预计领料,CAST((SELECT YJSC FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 预计生产,CAST((SELECT YJQG FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 预计请购,CAST((SELECT YJJH FROM V_WLPH WHERE TD004=D.ITEM_CODE) AS REAL) 预计进货 FROM MO_D A LEFT JOIN MO B ON A.MO_ID=B.MO_ID LEFT JOIN OPERATION C ON A.OPERATION_ID=C.OPERATION_ID LEFT JOIN ITEM D ON A.ITEM_ID=D.ITEM_BUSINESS_ID LEFT JOIN PROJECT E ON B.PROJECT_ID=E.PROJECT_ID WHERE B.DOC_DATE<='" + dateTimePicker2.Value.ToString() + "' AND B.DOC_DATE>='" + dateTimePicker1.Value.ToString() + "' AND (D.ITEM_NAME LIKE '%" + textBox5.Text + "%' OR D.ITEM_SPECIFICATION LIKE '%" + textBox5.Text + "%')";
            //textBox2.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl5.DataSource = "";
                this.gridControl5.Refresh();
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                this.gridControl5.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl5.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
